package com.hmall.search.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hmall.common.dto.PageDTO;
import com.hmall.search.dto.Item;
import com.hmall.search.dto.ItemDto;
import com.hmall.search.interceptor.UserInterceptor;
import com.hmall.search.mapper.SearchMapper;
import com.hmall.search.dto.SearchVo;
import com.hmall.search.service.SearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @Description SearchServiceImpl
 * @Author dmx
 * @Date 2024-11-14
 */
@Service
@Slf4j
public class SearchServiceImpl implements SearchService {
    @Autowired
    private SearchMapper searchMapper;

    /**
     * 查询过滤项
     * @param itemDto
     * @return
     */
    @Override
    public SearchVo searchFilters(ItemDto itemDto) {

        if (itemDto.getCategory()==null){
            List<String> category =searchMapper.searchCategory(itemDto);
            List<String> brand =searchMapper.searchBrand(itemDto);
            SearchVo searchVo = SearchVo.builder().category(category).brand(brand).build();
            return searchVo;
        }else {
            List<String> brand=searchMapper.selectBranchByCategory(itemDto.getCategory());
            return SearchVo.builder().brand(brand).build();
        }

    }

    /**
     * 分页查询
     * @param itemDto
     * @return
     */
//    @Autowired
//    private UserInterceptor userInterceptor;
    @Override
    public PageDTO<Item> list(ItemDto itemDto) {

        PageHelper.startPage(itemDto.getPage(), itemDto.getSize());
        //执行查询
        List<Item> itemList =searchMapper.getPage(itemDto);
        Page<Item> page= (Page<Item>) itemList;
        Long userId = UserInterceptor.getUserId();
        log.info("用户id为{}",userId);
        return new PageDTO<>(page.getTotal(),page.getResult());

    }
}
